/*
 * GameManager.java
 *
 * Created on 29 October 2006, 16:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package digitrix.fightclub.game;

//<editor-fold defaultstate="collapsed" desc="imports">
import digitrix.fightclub.data.DAO;
import digitrix.fightclub.data.PlayerDAO;
import digitrix.fightclub.network.ConnectionManager;
import digitrix.network.bt.BTManager;
import digitrix.network.bt.IBTCallback;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//</editor-fold>

/**
 *
 * @author Josh
 */
public class MenuManager extends GameCanvas implements Runnable, IManager, IBTCallback {
    
    private static final int KEY_DELAY = 350; 
    
    private static final int BGCOL_SPLASH = 0xffffff;
    private static final int BGCOL_MENU = 0x000000;
    
    private static final String IMGPATH_LOGO = "/digitrix/fightclub/res/digitrix.png";
    
    // game states
    public static final byte MENU_NO_STATE                  = -1; 
    public static final byte MENU_SPLASH                    = 20;
    public static final byte MENU_ADVERT                    = 21;
    public static final byte MENU_MAIN                      = 22;
    public static final byte MENU_HELP                      = 23;
    public static final byte MENU_ABOUT                     = 24;
    public static final byte MENU_PLAYERSTATS               = 25;
    public static final byte MENU_SUBMITSCORE               = 26;
    public static final byte MENU_SUBMITSCORE_TEMP          = 27;
    public static final byte MENU_LOGIN                     = 28;
    public static final byte MENU_NETSTART                  = 29;
    public static final byte MENU_NETEND                    = 30;
    public static final byte MENU_CHOICE                    = 31;
    public static final byte MENU_MESSAGE                   = 32;  
    public static final byte MENU_NEW_IDENTITY              = 33; 
    // bt menus 
    public static final byte MENU_BT_LOOK_OR_WAIT           = 34; // menu that asks the user whether they want to be a server (look for a fight) or client (wait for a fight)
    public static final byte MENU_BT_AVAILABLE_OPPONENTS    = 35; // menu that the server sees (list of available opponents)
    public static final byte MENU_BT_ACCEPT_DECLINE         = 36; // menu where the client user accepts or declines fight  
    public static final byte MENU_BT_SEARCHING              = 37;
    public static final byte MENU_BT_WAITING                = 38; 
    public static final byte MENU_BT_WAITING_CALLBACK       = 39; 
    public static final byte MENU_BT_READY                  = 40; 
    
    // MENU_BT_LOOK_OR_WAIT menu details
    public static final String[] SELECTION_LOOK_WAIT        = new String[]{"LOOK", "WAIT"}; 
    public static final byte CHOICE_LOOK                    = 0; // server
    public static final byte CHOICE_WAIT                    = 1; // client
    
    // MENU_BT_ACCEPT_DECLINE menu details
    public static final String[] SELECTION_ACCEPT_DECLINE   = new String[]{"ACCEPT", "DECLINE"}; 
    public static final byte CHOICE_ACCEPT                  = 0;
    public static final byte CHOICE_DECLINE                 = 1; 
            
    private static final int SPLASH_DELAY           = 2000; // time the splash screen is displayed
    private static final int ADVERT_DELAY           = 3000; // time the advert is displayed
    private static final int MESSAGE_DELAY          = 3000;
    private static final int NETWORK_RESULT_DELAY   = 4000;
    private static final int NETWORK_TIMEOUT        = 888000; 
    
    private static final byte MAX_FPS = 10;
    private static final int TICK_DELAY = 900; 
    
    public static final byte LOGIN_USERNAME_SEL = 0;
    public static final byte LOGIN_PASSWORD_SEL = 1;
    public static final byte LOGIN_SUBMIT_SEL   = 2; 
    public static final byte LOGIN_CANCEL_SEL   = 3;    
    
    public static final byte CHOICE_YES_SEL = 1;
    public static final byte CHOICE_NO_SEL = 0;
    
    public static final byte ACTION_SAVE_GAME = 0;
     
    public static final byte MENUITM_NEW_PRACTICE    = 0;
    public static final byte MENUITM_CHALLENGE       = 1;   // 
    public static final byte MENUITM_SELECT_PLAYER   = 2;
    public static final byte MENUITM_RESUME_GAME     = 3;
    public static final byte MENUITM_HELP            = 4;
    public static final byte MENUITM_SPLASH          = 5;
    public static final byte MENUITM_PLAYER_STATES   = 6;
    public static final byte MENUITM_SUBMIT_SCORE    = 7;
    public static final byte MENUITM_ABOUT           = 8;
    public static final byte MENUITM_EXIT            = 9;
    
    private static final String[] MENUITMS = { "PRACTICE", "CHALLENGE", "NEW IDENTITY", "RESUME GAME", "HELP", "SPLASH", "YOUR STATS", "SUBMIT SCORE", "ABOUT", "EXIT" };
    private static final String[] HELP_TEXT = {"FIGHT CLUB","", "ARE YOU READY", "TO FIGHT",
    "THE WORLDS", "TOUGEST?", "", "CONTROLS", "FIRE TO PUNCH", "FWD FIR 2 KICK", "BAK FIR 4 SPECIAL"};
    private static final String[] ABOUT_TEXT = {"DIGITRIX", "PRESENTS" ,"" , "ANOTHER GREAT", "PRODUCT","" , "FIGHTCLUB V2", "","CHECKOUT", "WWW.DIGITRIX.CO.NZ",
    "WHEN I GET THE SITE UP", "", "COPYRIGHT*2006", "ALL*RIGHTS*RESERVED"};
    
    private static MenuManager _instance;
    
    private Thread _menuThread = null;
    private Image _imgWallpaper = null;
    private Image _imgArrows = null;
    private Image _imgLogo = null;
    
    private boolean _running;
    
    // game variables
    private boolean _sleeping = false;
    private byte _currState = MENU_SPLASH; // goto MENU_BT_LOOK_OR_WAIT
    private byte  _prevState = MENU_SPLASH;
    private byte  _nextState = -1; 
    private long _stateTime = 0;
    private long _currTime = 0;
    private int _tick = 0; // general purpose tick
    private long _tickChanged = 0; // stores when the tick changed (incremented) 
    
    private String _tmpUsername = null;
    private String _tmpPassword = null;
    private String _tmpNotification = null;
    ConnectionManager _cxnManager = null;
    
    private int _selectedMenuItem = LOGIN_USERNAME_SEL;
    private int _selectedChoiceItem = CHOICE_YES_SEL;
    
    private PlayerDAO _playerDAO = null;
    private int _playerSelected = 1; //player id
    private String _playerName = null; 
    private int _playerStrength = 0;
    private int _playerSpeed = 0;         
    
    private int _opponentSelected = 2;    
    private Random _randOpponent = null;
    private String _opponentName = ""; 
    private int _opponentStrength = 0; 
    private int _opponentSpeed = 0; 
    private int _opponentFights = 0; 
    private BTManager _btManager = null; 
    private boolean _isServer = false; 
    
    private boolean _localReady = false; 
    private boolean _remoteReady = false; 
    
    private String[] _opponents = null; 
    
    private int _curMenuItem = 0;
    
    private int posScrollingY = 0;
    
    // key delay support variables 
    private long _keyPressedLast = 0; 
    
    /** Creates a new instance of GameManager */
    public MenuManager() {
        super( true );
        _instance = this;
        
        initPlayerDAO();         
        
        setCommandListener( FightClub.getInstance() );
        
        initMenu();
        
        // start games thread
        if ( _menuThread == null || !_menuThread.isAlive() ){
            _menuThread = new Thread( this );
            _running = true;
            _menuThread.start();
        }
    }
    
    public static MenuManager getInstance(){
        return _instance;
    }
    
    public void sleep(){
        _sleeping = true;
    }
    
    public void wakeup(){
        _currTime = System.currentTimeMillis();
        _sleeping = false;
        synchronized( this ){
            try{
                this.notify();
            } catch( Exception e ){
                System.out.println( "[ERR] MenuManager.wakeup(1); " + e.getMessage() );
            }
        }
    }
    
    private void initMenu(){
        _imgWallpaper = FightClub.getInstance().getWallpaper();
        _imgArrows = FightClub.getInstance().getArrows();
        
        try{
            _imgLogo = Image.createImage( IMGPATH_LOGO );
        } catch( Exception e ){}
        
    }
    
    /** 
     * Obtain reference to the playerdao singleton; if data exists then popupate the associated variables, if 
     * not then make sure that the player initilises themselves first ie don't allow them to practice or fight someone
     * else until they have selected their entered their name and selected their character. 
     **/
    private void initPlayerDAO(){
        // get player dao 
        _playerDAO = PlayerDAO.getInstance();   
        
        if( _playerDAO.isPlayerInit() ){
            // if successfully loaded then get the player details                         
            _playerSelected = _playerDAO.getPlayerID(); 
            _playerName = _playerDAO.getPlayerName(); 
            _playerStrength = _playerDAO.getStrenth();
            _playerSpeed = _playerDAO.getSpeed(); 
            
            FightClub.printDebug( "MenuManager", "initPlayerDAO", "ID: " + _playerSelected + 
                    ", Name: " + _playerName + ", Strength: " + _playerStrength + ", Speed: " + _playerSpeed );
        }
        else{
            // else only allow the player to access the 'create new character option'
            _curMenuItem = MENUITM_SELECT_PLAYER; 
        }
    }
    
    public void setState( byte newState ){
        System.out.println( "[INFO] MenuManager.setState(1); Updating Menu State to " + newState );
        
        _tick = 0; 
        _tickChanged = System.currentTimeMillis(); 
        _stateTime = 0;
        _nextState = -1; 
        _prevState = _currState;
        _currState = newState;
        
        if( _prevState == MENU_MESSAGE && newState != MENU_MESSAGE )
            _tmpNotification = null;
        
       
        switch( _currState ){
            case MENU_SPLASH:
                
                break;
            case MENU_ADVERT:
                
                break;
            case MENU_MAIN:
                
                break;
            case MENU_HELP:
                posScrollingY = getHeight() - 12;
                break;
            case MENU_ABOUT:
                posScrollingY = getHeight() - 12;
                break;
            case MENU_PLAYERSTATS:
                
                break;
            case MENU_LOGIN:
                _selectedMenuItem = LOGIN_USERNAME_SEL;
                break;
            case MENU_SUBMITSCORE:
                _cxnManager = FightClub.getInstance().openNewConnection();
                setState( MENU_NETSTART );
                _cxnManager.submitHighScore();
                break;
            case MENU_SUBMITSCORE_TEMP:
                _cxnManager = FightClub.getInstance().openNewConnection();
                setState( MENU_NETSTART );
                _cxnManager.submitHighScore(_tmpUsername, _tmpPassword );
                break;
            case MENU_NETSTART:
                
                break;
            case MENU_NETEND:
                
                break;
            case MENU_CHOICE:
                
                break;
            case MENU_MESSAGE:
                
                break;
            case MENU_BT_LOOK_OR_WAIT:
                _selectedChoiceItem = CHOICE_LOOK;
                break;
            case MENU_BT_AVAILABLE_OPPONENTS:
                _selectedChoiceItem = 0; 
                break; 
            case MENU_BT_ACCEPT_DECLINE:
                _selectedChoiceItem = CHOICE_ACCEPT; 
                break;
            case MENU_BT_SEARCHING:
                _tmpNotification = "SEARCHING"; 
                break;
            case MENU_BT_WAITING:
                _tmpNotification = "WAITING";                
                break;             
        }
    }
    
    private void setState( byte newState, byte nextState ){
        setState( newState );
        _nextState = nextState;
    }
    
    public void run() {
        _currTime = System.currentTimeMillis();
        
        long endTime;
        
        while( _running ){
            long elapsedTime = System.currentTimeMillis() - _currTime;
            _currTime += elapsedTime;
            
            // sleep game thread not focused or set to sleep
            if( _sleeping || FightClub.getActivatedDisplayable() != this && (_running) ){
                System.out.println( "[INFO] MenuManager.run(1); Going to sleep" );
                _sleeping = true;
                synchronized( this ){
                    try{
                        this.wait();
                    } catch( Exception e ){
                    }
                }
                System.out.println( "[INFO] MenuManager.run(2); Back to life!" );
            }
            
            if( !_running )
                break; 
            
            // process state
            _stateTime += elapsedTime;
            if( _currState == MENU_SPLASH ){
                if( _stateTime > SPLASH_DELAY )
                    setState( MENU_ADVERT );
            } else if( _currState == MENU_ADVERT ){
                if( _stateTime > ADVERT_DELAY )
                    setState( MENU_MAIN );
            } else if( _currState == MENU_MESSAGE ){
                if( _stateTime > MESSAGE_DELAY ){
                    if( _nextState != MENU_NO_STATE ){
                        setState( _nextState ); 
                    }
                    else if( _prevState == MENU_SUBMITSCORE || _prevState == MENU_NETSTART )
                        setState( MENU_MAIN );
                    else
                        setState( _prevState );
                }
            } else if( _currState == MENU_NETEND ){
                if( _stateTime > NETWORK_RESULT_DELAY )
                    setState( MENU_MAIN );
            } else if( _currState == MENU_NETSTART ){
                if( _stateTime > NETWORK_TIMEOUT ){
                    if( _cxnManager != null )
                        _cxnManager.Stop(); 
                    
                    // report timeout to user
                    _tmpNotification = "NETWORK TIMEOUT";
                    setState( MENU_MESSAGE ); 
                }
                    
            }  
            
            // tick manegement 
            if( _currState == MENU_BT_SEARCHING || _currState == MENU_BT_WAITING || _currState == MENU_BT_WAITING_CALLBACK ){
                if( (System.currentTimeMillis() - _tickChanged) > TICK_DELAY ){
                    _tick++;
                    _tickChanged = System.currentTimeMillis(); 
                }
            }
            
            // process game
            checkUserInput( elapsedTime );
            
            render();
            
            syncGameLoop( _currTime );
            
        }
        
        FightClub.printDebug( "MenuManager", "run", "MenuManager stopped" ); 
    }
    
    public void stop(){
        _running = false; 
         synchronized( this ){
            try{
                this.notify();
            } catch( Exception e ){}
        }
    }
    
    
    //<editor-fold defaultstate="collapsed" desc="helper methods">
    
    private void syncGameLoop( long currTime ){
        // sleep if necessary to make a smooth framerate
        long endTime = System.currentTimeMillis() - currTime;
        //System.out.println( "Start Time = " + startTime + " End Time = " + endTime );
        if (endTime < (1000 / MAX_FPS)) {
            try {
                // frames per second
                _menuThread.sleep((1000 / MAX_FPS) - endTime);
            } catch (Exception e) { }
        } else {
            try {
                _menuThread.yield();
            } catch (Exception e) { }
        }
    }
    
    private boolean isKeyHandlingAllowed(){
        if( System.currentTimeMillis() > (_keyPressedLast + KEY_DELAY) )
            return true; 
        else
            return false; 
    }
    
    private void checkUserInput( long elapsedTime ){
        if( !isKeyHandlingAllowed() )
            return; 
        
        int keyState = getKeyStates();                
        
        // set last time the key was pressed 
        _keyPressedLast = System.currentTimeMillis(); 
        
        switch( _currState ){
            case MENU_MAIN:
                if( (keyState & FIRE_PRESSED) != 0 ){                    
                    handleMainMenuSelection();                    
                } else if( (keyState & LEFT_PRESSED) != 0 ){
                    _curMenuItem--;                                        
                    
                    // skip resume game if we do not have a paused game
                    if( _curMenuItem == MENUITM_RESUME_GAME && !FightClub.getInstance().hasPausedGame() )
                        _curMenuItem--;
                    
                    // skip challenge (BT challenge) if the user has not selected a character 
                    if( _curMenuItem == MENUITM_CHALLENGE && !_playerDAO.isPlayerInit() )
                        _curMenuItem--; 
                    
                    // skip the practice if the user has not selected a character 
                    if( _curMenuItem == MENUITM_NEW_PRACTICE && !_playerDAO.isPlayerInit() )
                        _curMenuItem--; 
                    
                    if( _curMenuItem < 0 )
                        _curMenuItem = MENUITMS.length-1;
                } else if( (keyState & RIGHT_PRESSED) != 0 ){
                    _curMenuItem++;
                    
                     if( _curMenuItem >= MENUITMS.length )
                        _curMenuItem = 0;
                    
                    // skip the practice if the user has not selected a character 
                    if( _curMenuItem == MENUITM_NEW_PRACTICE && !_playerDAO.isPlayerInit() )
                        _curMenuItem++; 
                    
                    // skip challenge (BT challenge) if the user has not selected a character 
                    if( _curMenuItem == MENUITM_CHALLENGE && !_playerDAO.isPlayerInit() )
                        _curMenuItem++;                                         
                    
                    // skip resume game if we do not have a paused game
                    if( _curMenuItem == MENUITM_RESUME_GAME && !FightClub.getInstance().hasPausedGame() )
                        _curMenuItem++;                                       
                }
                break;
            case MENU_ABOUT:
            case MENU_HELP:
            case MENU_PLAYERSTATS:
            case MENU_NETEND:
                if( (keyState & FIRE_PRESSED) != 0 )
                    setState( MENU_MAIN );
                break;
            case MENU_LOGIN:
                if( (keyState & FIRE_PRESSED) != 0 ){
                    switch( _selectedMenuItem ){
                        case LOGIN_USERNAME_SEL:
                            FightClub.getInstance().activateUsernameTB();
                            break;
                        case LOGIN_PASSWORD_SEL:
                            FightClub.getInstance().activatePasswordTB();
                            break;
                        case LOGIN_SUBMIT_SEL:
                            if( _tmpUsername == null || _tmpUsername.toLowerCase().equals("enter username" ) ){
                                _tmpNotification = "USERNAME REQUIRED";
                            }
                            else if( _tmpPassword == null || _tmpPassword.toLowerCase().equals("enter password") ){
                                _tmpNotification = "PASSWORD REQUIRED";
                            }
                            else{
                                setState( MENU_SUBMITSCORE_TEMP );                                
                            }
                            break;
                        case LOGIN_CANCEL_SEL:
                            setState( MENU_MAIN ); 
                            break;
                    }
                }
                else if( (keyState & UP_PRESSED) != 0 ){
                    _selectedMenuItem--;
                    if( _selectedMenuItem < 0 )
                        _selectedMenuItem = 3; 
                }                                
                else if( (keyState & DOWN_PRESSED) != 0 ){
                    _selectedMenuItem++;
                    if( _selectedMenuItem > 3 )
                        _selectedMenuItem = 0; 
                }
                break;
            case MENU_BT_LOOK_OR_WAIT:
            case MENU_BT_ACCEPT_DECLINE:                    
                if( (keyState & FIRE_PRESSED) != 0 ){
                    if( _currState == MENU_BT_LOOK_OR_WAIT ){
                        if ( _selectedChoiceItem == CHOICE_LOOK ){                            
                            FightClub.printDebug( "MenuManager", "checkUserIinput", "Starting server service" );
                            setState( MENU_BT_SEARCHING );
                            
                            // set the BT Manager to start searching for available devices running this service/game 
                            setIsServer(true);
                            getBTManager().setIsServer( isIsServer());
                            try{
                                getBTManager().startService( _playerName );
                            }
                            catch( Exception e ){
                                FightClub.printError( "MenuManager.checkUserInput(MENU_BT_LOOK_OR_WAIT)", e.toString() );
                            }
                        }
                        else if( _selectedChoiceItem == CHOICE_WAIT ){                            
                            FightClub.printDebug( "MenuManager", "checkUserIinput", "Starting client service" );
                            setState( MENU_BT_WAITING );
                                                        
                            // set the BT Manager to listen for any server searching for this service/game                           
                            setIsServer(false);
                            getBTManager().setIsServer( isIsServer());
                            try{
                                getBTManager().startService( _playerName );
                            }
                            catch( Exception e ){
                                FightClub.printError( "MenuManager.checkUserInput(MENU_BT_LOOK_OR_WAIT)", e.toString() );
                            }
                            
                        }
                    }
                    else if( _currState == MENU_BT_ACCEPT_DECLINE ){
                       if ( _selectedChoiceItem == CHOICE_ACCEPT ){
                           FightClub.printDebug( "MenuManager", "checkUserInput()", "_selectedChoiceItem == CHOICE_ACCEPT" );
                           // build player details and send accept message to the server                              
                           getBTManager().acceptChallenge( getPlayerDetailsForBTDispatcher() );
                           
                           _tmpNotification = "WAITING";
                           setState( MENU_BT_WAITING_CALLBACK ); 
                        }
                        else if( _selectedChoiceItem == CHOICE_DECLINE ){
                           // send decline message to server
                           getBTManager().declineChallenge(); 
                        } 
                    }
                }
                else if( (keyState & UP_PRESSED) != 0 || (keyState & RIGHT_PRESSED) != 0 ){
                    _selectedChoiceItem--;
                    if( _selectedChoiceItem< 0 )
                        _selectedChoiceItem = 1; 
                }                                
                else if( (keyState & DOWN_PRESSED) != 0 || (keyState & LEFT_PRESSED) != 0){
                    _selectedChoiceItem++;
                    if( _selectedChoiceItem > 1 )
                        _selectedChoiceItem = 0; 
                }
                break;
            case MENU_BT_AVAILABLE_OPPONENTS:
                if( (keyState & UP_PRESSED) != 0 || (keyState & RIGHT_PRESSED) != 0 ){
                    _selectedChoiceItem--;
                    if( _selectedChoiceItem< 0 )
                        _selectedChoiceItem = _opponents.length-1; 
                }                                
                else if( (keyState & DOWN_PRESSED) != 0 || (keyState & LEFT_PRESSED) != 0){
                    _selectedChoiceItem++;
                    if( _selectedChoiceItem > _opponents.length-1 )
                        _selectedChoiceItem = 0; 
                }
                else if( (keyState & FIRE_PRESSED) != 0 ){
                    _tmpNotification = "WAITING";
                    setState( MENU_BT_WAITING_CALLBACK ); 
                    
                    getBTManager().requestChallenege( _opponents[_selectedChoiceItem] ); 
                }
                break;   
            case MENU_BT_READY:
            {
                if( (keyState & FIRE_PRESSED) != 0 ){
                    if( !_localReady && _btManager.getState() != BTManager.STATE_LAG_TESTING ){
                        FightClub.printDebug( "MenuManager", "checkUserInput", "Local player is ready" ); 
                        
                        _tmpNotification = "TESTING CXN"; 
                        
                        // send request to remote user - start the lag test                     
                        _btManager.startLagTest(); 
                    }                    
                }
                break; 
            }
        }
        
    }        
    
    private void readyToFight(){
        if( _localReady && _remoteReady ){
            FightClub.printDebug( "MenuManager", "readyToFight", "Game On!" ); 
            render(); // (hack-fix - render screen because for some reason when swapping screens the menumanager displays a random state screen)
            FightClub.getInstance().activateGameManager( true );
        }
    }
    
    private String getPlayerDetailsForBTDispatcher(){                                            
        return _playerName + "," + _playerSelected + "," + 
                _playerStrength + "," + _playerSpeed + "," + PlayerDAO.getInstance().getPlayerFights();
    }
    
    /**
     * playerDetails: e.g. "name,playerid,strength,speed,fights"
     **/
    private void setOpponentDetailsFromBTListener( String playerDetails ){
                
        _opponentFights = Integer.parseInt( playerDetails.substring( playerDetails.lastIndexOf( ',' )+1, playerDetails.length() ) );
        _opponentName = playerDetails.substring( 0, playerDetails.lastIndexOf( ',' ) );        
        
        _opponentSpeed = Integer.parseInt( _opponentName.substring( _opponentName.lastIndexOf( ',' )+1, _opponentName.length() ) );
        _opponentName = _opponentName.substring( 0, _opponentName.lastIndexOf( ',' ) );         
        
        _opponentStrength = Integer.parseInt( _opponentName.substring( _opponentName.lastIndexOf( ',' )+1, _opponentName.length() ) );
        _opponentName = _opponentName.substring( 0, _opponentName.lastIndexOf( ',' ) );        
        
        _opponentSelected = Integer.parseInt( _opponentName.substring( _opponentName.lastIndexOf( ',' )+1, _opponentName.length() ) );
        _opponentName = _opponentName.substring( 0, _opponentName.lastIndexOf( ',' ) );                 
        
    }
    
    private void handleMainMenuSelection(){
        switch( _curMenuItem ){
            
            case MENUITM_NEW_PRACTICE:
                FightClub.getInstance().setGameMode( FightClub.GAME_MODE_PRACTICE ); 
                selectOpponent();
                FightClub.getInstance().setGameMode( FightClub.GAME_MODE_PRACTICE );
                FightClub.getInstance().activateGameManager( true );
                break;
            case MENUITM_CHALLENGE:
                setState( MENU_BT_LOOK_OR_WAIT );
                // set the game mode 
                FightClub.getInstance().setGameMode( FightClub.GAME_MODE_CHALLENGE );                
                break; 
            case MENUITM_SELECT_PLAYER:
                // TODO: if user has already selected a player then warn the user that if the 
                // continue all states will be lost
                setState( MENU_NEW_IDENTITY ); 
                break; 
            case MENUITM_RESUME_GAME:
                FightClub.getInstance().activateGameManager( false );
                break;
            case MENUITM_SPLASH:
                setState( MENU_SPLASH );
                break;
            case MENUITM_ABOUT:
                setState( MENU_ABOUT );
                break;
            case MENUITM_HELP:
                setState( MENU_HELP );
                break;
            case MENUITM_PLAYER_STATES:
                setState( MENU_PLAYERSTATS );
                break;
            case MENUITM_SUBMIT_SCORE:
                //if( DAO.getPlayerWins() == 0 && DAO.getPlayerLoses() == 0 && DAO.getPlayerDraws() == 0 ){
                //    _tmpNotification = "DONT WANT TO FIGHT?";
                //    setState( MENU_MESSAGE );
                //}
                //else if( DAO.getUserName() == null )
                if( DAO.getUserName() == null )
                    setState( MENU_LOGIN );
                else
                    setState( MENU_SUBMITSCORE );
                break;            
            case MENUITM_EXIT:
                FightClub.getInstance().exit();
                break;
        }
    }
    
    private void selectOpponent(){
        if( _randOpponent == null )
            _randOpponent = new Random();
        
        while( (_opponentSelected = _randOpponent.nextInt( 4 )) <= 0);
        
        _opponentName = "PUTER";        
    }
    
    private void render(){
        if( FightClub.getActivatedDisplayable() != this )
            return;
        
        Graphics g = getGraphics();
        
        // clear background
        g.setColor( 0xffffff );
        g.fillRect( 0, 0, getWidth(), getHeight() );                
        
        // draw wallpaper
        switch( _currState ){
            case MENU_SPLASH:
                renderSplash( g );
                break;
            case MENU_ADVERT:
                renderAdvert( g );
                break;
            case MENU_MAIN:
                renderMainMenu( g );
                break;
            case MENU_HELP:
                renderHelp( g );
                break;
            case MENU_ABOUT:
                renderAbout( g );
                break;
            case MENU_PLAYERSTATS:
                renderPlayerStats( g );
                break;
            case MENU_LOGIN:
                renderLogin( g );
                break;                
            case MENU_NETSTART:
                renderNetStatus( g ); 
                break;
            case MENU_NETEND:
                renderNetworkResult( g ); 
                break;
            case MENU_CHOICE:
                
                break;
            case MENU_MESSAGE:
                renderMessage( g );
                break;
            case MENU_BT_LOOK_OR_WAIT:
                renderChoiceMenu( g ); 
                break;
            case MENU_BT_WAITING:
            case MENU_BT_WAITING_CALLBACK: 
            case MENU_BT_SEARCHING:
                renderLoading( g ); 
                break;        
            case MENU_BT_READY:
                renderReady( g ); 
                break;
            case MENU_BT_AVAILABLE_OPPONENTS:
                renderOpponents( g ); 
                break;
            case MENU_BT_ACCEPT_DECLINE:
                renderChoiceMenu( g ); 
                break;                
        }
        
        flushGraphics();
        
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Rendering methods">
    private void renderSplash( Graphics g ){
        g.setColor( BGCOL_SPLASH );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        g.drawImage( _imgLogo, getWidth()/2, getHeight()/2, Graphics.HCENTER | Graphics.VCENTER );
    }
    
    private void renderAdvert( Graphics g ){
        Image ad = DAO.getClientImage();
        
        if( ad == null ){
            setState( MENU_MAIN );
            return;
        }
        
        // clear screen
        g.setColor( BGCOL_SPLASH );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // get x coordinate
        int x = (getWidth()-ad.getWidth())/2;
        if( x < 0 )
            x = 0;
        
        // get x coordinate
        int y = (getHeight()-ad.getHeight())/2;
        if( y < 0 )
            y = 0;
        
        // paint ad
        g.drawImage( ad, x, y, Graphics.TOP | Graphics.LEFT );
        
        // print whatever message
//        y = (getHeight()-ad.getHeight())/2;
//        if( y >= 20 ){
//            printToScreen( g, getWidth()/2, getHeight()-12, "PRESENT", false );
//        }
        
    }
    
    public void renderHelp( Graphics g ){
        renderScrollingText( HELP_TEXT, g );
    }
    
    public void renderAbout( Graphics g ){
        renderScrollingText( ABOUT_TEXT, g );
    }
    
    public void renderScrollingText( String[] text, Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // if the bottom line is above the top of the screen, then reset the variable/pointer
        // posScrollingY to the screensHeight - 10;
        if( posScrollingY + (text.length * 10) < 10 )
            posScrollingY = getHeight() -10;
        
        for( int i = 0; i < text.length; i ++ ){
            FightClub.getInstance().getGameFont().drawSmallString( text[i], (getWidth()-(text[i].length()*13))/2, posScrollingY + (i*15), g );
        }
        
        posScrollingY -= 1;
    }
    
    private void renderPlayerStats( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // try and load player stats
        DAO.loadHighScores();
        
        String strWins = "WON " + _playerDAO.getPlayerWins();
        String strLoses = "LOSES " + _playerDAO.getPlayerLoses();
        String strDraws = "DRAWS " + _playerDAO.getPlayerDraws();
        
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        int posY = 40+_imgWallpaper.getHeight()+16;
        //int posY = 10;
        
        FightClub.getInstance().getGameFont().drawLargeString( "YOUR STATS", (getWidth()-("YOUR STATS".length()*13))/2, posY, g );
                        
        // player name
        posY += 20;
        String txt = _playerDAO.getPlayerName(); 
        FightClub.getInstance().getGameFont().drawSmallString( txt, (getWidth()-(txt.length()*13))/2, posY, g );
        
        // player strength 
        posY += 20;
        txt = "STRENGTH: " + _playerDAO.getStrenth(); 
        FightClub.getInstance().getGameFont().drawSmallString( txt, (getWidth()-(txt.length()*13))/2, posY, g );
        
        // player speed 
        posY += 20;
        txt = "Speed: " + _playerDAO.getSpeed(); 
        FightClub.getInstance().getGameFont().drawSmallString( txt, (getWidth()-(txt.length()*13))/2, posY, g );
        
        // wins
        posY += 20;
        FightClub.getInstance().getGameFont().drawSmallString( strWins, (getWidth()-(strWins.length()*13))/2, posY, g );
        
        // loses
        posY += 20;
        FightClub.getInstance().getGameFont().drawSmallString( strLoses, (getWidth()-(strLoses.length()*13))/2, posY, g );
        
        // draws
        posY += 20;
        FightClub.getInstance().getGameFont().drawSmallString( strDraws, (getWidth()-(strDraws.length()*13))/2, posY, g );
        
    }
    
    private void renderMainMenu( Graphics g ){
        // clear background
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        // paint up arrow
        int pos1 = getWidth()/2 - (MENUITMS[_curMenuItem].length()*7 + 14);
        int pos2 = getWidth()/2 - MENUITMS[_curMenuItem].length()*6;
        int pos3 = getWidth()/2 + MENUITMS[_curMenuItem].length()*8;
        
        int pos4 = 40+_imgWallpaper.getHeight()+16;
        
        int vTest = getHeight()-5;
        while( pos4>vTest ){ pos4--; }
        
        g.setClip( pos1, pos4, 14, 12);
        g.drawImage( _imgArrows, pos1, pos4, Graphics.TOP | Graphics.LEFT );
        
        g.setClip( pos3, pos4, 14, 12);
        g.drawImage( _imgArrows, pos3-14, pos4, Graphics.TOP | Graphics.LEFT );
        
        g.setClip( 0, 0, getWidth(), getHeight() );
        FightClub.getInstance().getGameFont().drawLargeString( MENUITMS[_curMenuItem], pos2, pos4, g );
        
    }
    
    public void renderMessage( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        int posY = 40+_imgWallpaper.getHeight()+16;
        //int posY = getHeight()/2-4;
        
        if( _tmpNotification.length()*FightClub.getInstance().getGameFont().getSmallFontsWidth() > getWidth() ){
            // TODO:
            FightClub.getInstance().getGameFont().drawSmallString( _tmpNotification, (getWidth()-(_tmpNotification.length()*13))/2, posY, g );
        } else{
            FightClub.getInstance().getGameFont().drawSmallString( _tmpNotification, (getWidth()-(_tmpNotification.length()*13))/2, posY, g );
        }
    }
    
    private void renderLoading( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        String dots ="";
        if( _tick == 1 )
            dots = ".";     
        else if( _tick == 2 )
            dots = ".."; 
        else if( _tick == 3 )
            dots = "..."; 
        else if( _tick > 3 ){
            _tickChanged = System.currentTimeMillis(); 
            _tick = 0; // reset tick
        }
        
        int posY = getHeight()/2-4;
        
        if( _tmpNotification.length()*FightClub.getInstance().getGameFont().getSmallFontsWidth() > getWidth() ){
            // TODO:
            FightClub.getInstance().getGameFont().drawSmallString( _tmpNotification + dots, (getWidth()-(_tmpNotification.length()*13))/2, posY, g );
        } else{
            FightClub.getInstance().getGameFont().drawSmallString( _tmpNotification + dots, (getWidth()-(_tmpNotification.length()*13))/2, posY, g );
        }
    }
    
    private void renderReady( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );                
                
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        int posY = 40+_imgWallpaper.getHeight()+16;
        //int posY = getHeight()/2-4;
        
        if( _localReady ){
            FightClub.getInstance().getGameFont().drawSmallString( "WAITING FOR", (getWidth()-("WAITING FOR".length()*13))/2, posY, g );
            posY += 16; 
            FightClub.getInstance().getGameFont().drawSmallString( "OPPONENT", (getWidth()-("OPPONENT".length()*13))/2, posY, g );
        }
        else{
            FightClub.getInstance().getGameFont().drawSmallString( "PRESS FIRE", (getWidth()-("PRESS FIRE".length()*13))/2, posY, g );
            posY += 16; 
            FightClub.getInstance().getGameFont().drawSmallString( "WHEN READY", (getWidth()-("WHEN READY".length()*13))/2, posY, g );
        }        
    }
    
    private void renderChoiceMenu( Graphics g ){
        // clear background
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );                
        
        if( _localReady && _remoteReady )
            return; 
        
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        String menuItem = "BUGGER";
        int pos1 = 0;
        int pos2 = 0;
        int pos3 = 0; 
        
        if( _currState == MENU_BT_LOOK_OR_WAIT ){
            menuItem = SELECTION_LOOK_WAIT[_selectedChoiceItem];
            // paint up arrow
            pos1 = getWidth()/2 - (SELECTION_LOOK_WAIT[_selectedChoiceItem].length()*7 + 14);
            pos2 = getWidth()/2 - SELECTION_LOOK_WAIT[_selectedChoiceItem].length()*6;
            pos3 = getWidth()/2 + SELECTION_LOOK_WAIT[_selectedChoiceItem].length()*8;
        }
        else if( _currState == MENU_BT_ACCEPT_DECLINE ){
            menuItem = SELECTION_ACCEPT_DECLINE[_selectedChoiceItem];        
            pos1 = getWidth()/2 - (SELECTION_ACCEPT_DECLINE[_selectedChoiceItem].length()*7 + 14);
            pos2 = getWidth()/2 - SELECTION_ACCEPT_DECLINE[_selectedChoiceItem].length()*6;
            pos3 = getWidth()/2 + SELECTION_ACCEPT_DECLINE[_selectedChoiceItem].length()*8;
        }                
                
        int pos4 = 40+_imgWallpaper.getHeight()+16;
        
        if( _currState == MENU_BT_ACCEPT_DECLINE ){             
            String title = "FIGHT " + _opponentName;            
            FightClub.getInstance().getGameFont().drawLargeString( title, (getWidth()-(title.length()*13))/2, pos4, g );        
            pos4 += 30;
        }
        
        int vTest = getHeight()-5;
        while( pos4>vTest ){ pos4--; }
        
        g.setClip( pos1, pos4, 14, 12);
        g.drawImage( _imgArrows, pos1, pos4, Graphics.TOP | Graphics.LEFT );
        
        g.setClip( pos3, pos4, 14, 12);
        g.drawImage( _imgArrows, pos3-14, pos4, Graphics.TOP | Graphics.LEFT );
        
        g.setClip( 0, 0, getWidth(), getHeight() );                
        
        FightClub.getInstance().getGameFont().drawLargeString( menuItem, pos2, pos4, g );        
    }
    
    private void renderOpponents( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // paint logo
        g.drawImage( _imgWallpaper, (getWidth()-_imgWallpaper.getWidth())/2,  20, Graphics.TOP | Graphics.LEFT );
        
        int curPosY = 20+_imgWallpaper.getHeight()+16;
        
        //int curPosY = 20;
        int curPosX = getWidth()/2;
        
        // draw title
        FightClub.getInstance().getGameFont().drawLargeString( "OPPONENTS", (getWidth()-("OPPONENTS".length()*13))/2, curPosY, g );
        g.setClip( 0, 0, getWidth(), getHeight() );
        g.setColor( 0xffffff );
        g.drawLine( 25, curPosY+FightClub.getInstance().getGameFont().getLargeFontsHeight()+2, getWidth() -25, curPosY+FightClub.getInstance().getGameFont().getLargeFontsHeight()+2 );                
        
        curPosY += 30;
        
        // draw the name of each opponent
        for( int i=0; i<_opponents.length; i++ ){            
            
            FightClub.getInstance().getGameFont().drawSmallString( _opponents[i], (getWidth()-(_opponents[i].length()*13))/2, curPosY, g );
            
            // draw rectangle around username field if currently selected
            if( _selectedChoiceItem == i ){
                g.setClip( 0, 0, getWidth(), getHeight() );
                g.setColor( 0xffffff );
                g.drawRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );            
            }
            
            // update the y position 
            curPosY += 20;
        }                                
                
    }
    
    public void renderLogin( Graphics g ){
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        int curPosY = 20;
        int curPosX = getWidth()/2;
        
        // draw title
        FightClub.getInstance().getGameFont().drawLargeString( "LOGIN", (getWidth()-("LOGIN".length()*13))/2, curPosY, g );
        g.setClip( 0, 0, getWidth(), getHeight() );
        g.setColor( 0xffffff );
        g.drawLine( 25, curPosY+FightClub.getInstance().getGameFont().getLargeFontsHeight()+2, getWidth() -25, curPosY+FightClub.getInstance().getGameFont().getLargeFontsHeight()+2 );
        
        // draw name details
        curPosY += 40;
        FightClub.getInstance().getGameFont().drawSmallString( "USERNAME", (getWidth()-("USERNAME".length()*13))/2, curPosY, g );
        curPosY += 20;
        if( _tmpUsername != null )
            FightClub.getInstance().getGameFont().drawSmallString( _tmpUsername, (getWidth()-(_tmpUsername.length()*13))/2, curPosY, g );
        else
            FightClub.getInstance().getGameFont().drawSmallString( "ENTER USERNAME", (getWidth()-("ENTER USERNAME".length()*13))/2, curPosY, g );
        
        // draw rectangle around username field if currently selected
        if( _selectedMenuItem == LOGIN_USERNAME_SEL ){
            g.setClip( 0, 0, getWidth(), getHeight() );
            g.setColor( 0xffffff );
            g.drawRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );            
        }
        
        // draw password details
        curPosY += 40;
        FightClub.getInstance().getGameFont().drawSmallString( "PASSWORD", (getWidth()-("PASSWORD".length()*13))/2, curPosY, g );
        curPosY += 20;
        if( _tmpPassword != null )
            FightClub.getInstance().getGameFont().drawSmallString( _tmpPassword, (getWidth()-(_tmpPassword.length()*13))/2, curPosY, g );
        else
            FightClub.getInstance().getGameFont().drawSmallString( "ENTER PASSWORD", (getWidth()-("ENTER PASSWORD".length()*13))/2, curPosY, g );
        
        // draw rectangle around password field if currently selected
        if( _selectedMenuItem == LOGIN_PASSWORD_SEL ){
            g.setClip( 0, 0, getWidth(), getHeight() );
            g.setColor( 0xffffff );
            g.drawRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );
        }
        
        // draw login buttons
        // draw submit button
        curPosY += 25;
        
        g.setClip( 0, 0, getWidth(), getHeight() );
        g.setColor( 0xcccccc );
        g.fillRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );
        
        FightClub.getInstance().getGameFont().drawSmallString( "LOGIN", (getWidth()-("LOGIN".length()*13))/2, curPosY, g );                
        
        // draw rectangle around login button if currently selected
        if( _selectedMenuItem == LOGIN_SUBMIT_SEL ){
            g.setClip( 0, 0, getWidth(), getHeight() );
            g.setColor( 0xffffff );
            g.drawRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );
        }
        
        // draw cancel button
        curPosY += 18;
        
        g.setClip( 0, 0, getWidth(), getHeight() );
        g.setColor( 0xcccccc );
        g.fillRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );
        
        FightClub.getInstance().getGameFont().drawSmallString( "CANCEL", (getWidth()-("CANCEL".length()*13))/2, curPosY, g );
        
        // draw rectangle around login button if currently selected
        if( _selectedMenuItem == LOGIN_CANCEL_SEL ){
            g.setClip( 0, 0, getWidth(), getHeight() );
            g.setColor( 0xffffff );
            g.drawRoundRect( 25, curPosY-6, getWidth()-50, FightClub.getInstance().getGameFont().getSmallFontsHeight()+7, 5, 5 );
        }
        
        
        // print a notification (maybe a red background)
        if( _tmpNotification != null ){
            curPosY += 20;
            FightClub.getInstance().getGameFont().drawSmallString( _tmpNotification, (getWidth()-(_tmpNotification.length()*13))/2, curPosY, g );
        }
    }
    
    private void renderNetStatus( Graphics g ){        
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        int posY = getHeight()/2-4;        
        
        // draw status
        if( _cxnManager == null ){
            setState( MENU_NETEND );
        } else{
            switch( _cxnManager.getResponse() ){
                case ConnectionManager.RES_CONNECTING:
                    FightClub.getInstance().getGameFont().drawSmallString( "CONNECTING", (getWidth()-("CONNECTING".length()*13))/2, posY, g );
                    break;
                case ConnectionManager.RES_SUCCESS_CXN:
                    FightClub.getInstance().getGameFont().drawSmallString( "UPLOADING SCORE", (getWidth()-("UPLOADING SCORE".length()*13))/2, posY, g );
                    break;
                default:
                    FightClub.getInstance().getGameFont().drawSmallString( "TRYING", (getWidth()-("TRYING".length()*13))/2, posY, g );
            }
        }        
    }
    
    private void renderNetworkResult( Graphics g ){
        
        int posY = getHeight()/2;       
        int pos = 0; 
        
        g.setColor( BGCOL_MENU );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        
        // draw network result
        if( _cxnManager != null ){
            switch( _cxnManager.getResponse() ){                
                case ConnectionManager.RES_SUCCESS:
                    pos = _cxnManager.getPosition();
                    if( pos > 0 ){
                        if( pos <= 10 )
                            FightClub.getInstance().getGameFont().drawSmallString( "TOP TEN", (getWidth()-("TOP TEN".length()*13))/2, posY, g );
                        else if( pos <= 15 )
                            FightClub.getInstance().getGameFont().drawSmallString( "GOOD STUFF", (getWidth()-("GOOD STUFF".length()*13))/2, posY, g );
                        else if( pos > 100 )
                            FightClub.getInstance().getGameFont().drawSmallString( "YOU SUCK", (getWidth()-("YOU SUCK".length()*13))/2, posY, g );
                        else
                            FightClub.getInstance().getGameFont().drawSmallString( "YOUR " + pos + "TH", (getWidth()-(("YOUR " + pos + "TH").length()*13))/2, posY, g );                          
                    } else{
                        FightClub.getInstance().getGameFont().drawSmallString( "SCORE UPLOADED", (getWidth()-(("SCORE UPLOADED").length()*13))/2, posY, g );                          
                    }                                        
                    break;
                    
                case ConnectionManager.RES_FAIL_CXN:
                    FightClub.getInstance().getGameFont().drawSmallString( "CONNECTION FAILED", (getWidth()-("CONNECTION FAILED".length()*13))/2, posY, g );
                    break;
                    
                case ConnectionManager.RES_FAIL_NOSCORE:
                    FightClub.getInstance().getGameFont().drawSmallString( "NO SCORE", (getWidth()-("NO SCORE".length()*13))/2, posY, g );     
                    break;
                    
                case ConnectionManager.RES_FAIL_USERREG:
                    posY -= 20;
                    FightClub.getInstance().getGameFont().drawSmallString( "NOT REGISTERED", (getWidth()-("NOT REGISTERED".length()*13))/2, posY, g );     
                    posY += 10;
                    FightClub.getInstance().getGameFont().drawSmallString( "REGISTER AT", (getWidth()-("REGISTER AT".length()*13))/2, posY, g );           
                    posY += 10;
                    FightClub.getInstance().getGameFont().drawSmallString( "WWW.DIGITRIX.CO.NZ", (getWidth()-("WWW.DIGITRIX.CO.NZ".length()*13))/2, posY, g );     
                    posY += 10;
                    FightClub.getInstance().getGameFont().drawSmallString( "AND BE IN TO WIN", (getWidth()-("AND BE IN TO WIN".length()*13))/2, posY, g );     
                    break;
            }
        } else{
            // no connection manager found
            FightClub.getInstance().getGameFont().drawSmallString( "ERROR", (getWidth()-("ERROR".length()*13))/2, posY, g );     
            
        }
        
        // near the buttom indicate that the user can press any key to continue
        // length of 'press fire to continue' =
        //printToScreen( g, getWidth()/2, (getHeight() - getHeight()/5), _pressFireToContinue, false );
        
    }
    
    //</editor-fold>        
        
    //<editor-fold defaultstate="collapsed" desc="getters/setters">
    
    public boolean isIsServer() {
        return _isServer;
    }

    public void setIsServer(boolean isServer) {
        this._isServer = isServer;
        FightClub.getInstance().setServer( _isServer ); 
    }
    
    private BTManager getBTManager(){
        if( _btManager == null )
            _btManager = FightClub.getInstance().getBTManager();
        
        return _btManager; 
    }
    
    public int getPlayerSelected() {
        return _playerSelected;
    }
    
    public void setPlayerSelected(int playerSelected) {
        this._playerSelected = playerSelected;
    }
    
    public String getPlayerName(){
        return _playerName; 
    }
    
    public int getPlayerStrenth(){
        return _playerStrength; 
    }
    
    public int getPlayerSpeed(){
        return _playerSpeed; 
    }
    
    public int getOpponentSelected() {
        return _opponentSelected;
    }
    
    public void setOpponentSelected(int opponentSelected) {
        this._opponentSelected = opponentSelected;
    }
    
    public String getOpponentName(){
        return _opponentName; 
    }
    
    public void setOpponentName( String name ){
        _opponentName = name.toUpperCase(); 
    }
    
    public int getOpponentStrength(){
        return _opponentStrength; 
    }
    
    public void setOpponentStrength( int strength ){
        _opponentStrength = strength; 
    }
    
    public int getOpponentSpeed(){
        return _opponentSpeed;
    }
    
    public void setOpponentSpeed( int speed ){
        _opponentSpeed = speed; 
    }
    
    public int getOpponentFights(){
        return _opponentFights; 
    }
    
    public void setOpponentFights( int fights ){
        _opponentFights = fights; 
    }
    
    public void setNotification( String notification ){
        _tmpNotification = notification;
    }
    
    public void setTempUsername( String un ){
        _tmpUsername = un;
    }
    
    public void setTempPassword( String pw ){
        _tmpPassword = pw; 
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="IBTCallback implementation">
    public void btSetStatus(String status) {
        //TODO: display the actual message to the user
        _tmpNotification = status; 
        FightClub.getInstance().printDebug( "GameManager", "btSetStatus", status ); 
    }

    public void btPlayerDetails(String details) {
        FightClub.printDebug( "MenuManager", "btPlayerDetails", "Player details retrieved: " + details );
        setOpponentDetailsFromBTListener( details ); 
        if( _isServer )
            _btManager.dispatchPlayerDetails( BTManager.EVT_PLAYER_INFO, getPlayerDetailsForBTDispatcher() ); 
        
        setState( MENU_BT_READY );         
    }
    
    public void btFightRequest(String playersName) {
        FightClub.printDebug( "MenuManager", "btFightRequest", playersName + " would like to fight" ); 
        _opponentName = playersName;         
        setState( MENU_BT_ACCEPT_DECLINE );                
    }

    public void btEvent(byte evtType, int evtDetails) {
        if( evtType == BTManager.EVT_READY ){
            FightClub.printDebug( "MenuManager", "btEvent", "Remote player is ready" ); 
            _remoteReady = true;             
            
            readyToFight(); 
        }  
        else if( evtType == BTManager.EVT_LOCAL_LAG_TEST_FINISHED ){
            _localReady = true;            
            _btManager.dispatchNonParamEvent( BTManager.EVT_READY );             
            
            readyToFight();
        }  
    }
    
    public void btPlayersFound( String[] players ){
        FightClub.printDebug( "MenuManager", "btPlayersFound", players.length + " players found" );
        
        _opponents = players; 
        setState( MENU_BT_AVAILABLE_OPPONENTS ); 
    }        
    
    public void btSyncEvent(int posX, int posY, int health) {
    }
    //</editor-fold>         
}
